//
//  XWDefaultInfiniteIndicator.m
//  Youpin
//
//  Created by xiaowuxiaowu on 2017/4/5.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "XWDefaultInfiniteIndicator.h"

@implementation XWDefaultInfiniteIndicator
-(id)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        _loadingView = [[XWWaveLoadingView alloc]initWithFrame:frame];
        [self addSubview:_loadingView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.center = CGPointMake(CGRectGetMidX(self.superview.bounds), CGRectGetMidY(self.superview.bounds));
}

- (void)startAnimating {
    [_loadingView startLoading];
}

- (void)stopAnimating {
    [_loadingView stopLoading];
}



- (void)pullToRefreshBackgroundView:(INSPullToRefreshBackgroundView *)pullToRefreshBackgroundView didChangeState:(INSPullToRefreshBackgroundViewState)state {
    [self handleStateChange:state];
}

- (void)pullToRefreshBackgroundView:(INSPullToRefreshBackgroundView *)pullToRefreshBackgroundView didChangeTriggerStateProgress:(CGFloat)progress {
    [self handleProgress:progress forState:pullToRefreshBackgroundView.state];
}

- (void)handleProgress:(CGFloat)progress forState:(INSPullToRefreshBackgroundViewState)state {
    if (progress > 0 && state == INSPullToRefreshBackgroundViewStateNone) {
        
    }
    
    [_loadingView updateWaveProgress:progress];
}
- (void)handleStateChange:(INSPullToRefreshBackgroundViewState)state {
    if (state == INSPullToRefreshBackgroundViewStateNone) {
        [self stopAnimating];
    } else {
        [self startAnimating];
    }
}

@end




static NSString* const INSInfiniteIndicatorSpinAnimationKey = @"SpinAnimation";

@interface XWCircleInfiniteIndicator ()

@property (nonatomic) CAShapeLayer* circle;
@property BOOL animating;
@end

@implementation XWCircleInfiniteIndicator

- (id)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        self.layer.contentsScale = [UIScreen mainScreen].scale;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.center = CGPointMake(CGRectGetMidX(self.superview.bounds), CGRectGetMidY(self.superview.bounds));
}


- (void)didMoveToWindow {
    // CoreAnimation animations are removed when view goes offscreen.
    // So we have to restart them when view reappears.
    if(self.window && self.animating) {
        [self startAnimating];
    }
}

- (CAShapeLayer*)circle {
    if(!_circle) {
        _circle = [CAShapeLayer layer];
        _circle.fillColor = [UIColor colorWithRed:0.173 green:0.243 blue:0.314 alpha:1].CGColor; /*#2c3e50*/
        _circle.contentsScale = self.layer.contentsScale;
        [self.layer addSublayer:_circle];
    }
    return _circle;
}

- (void)startAnimating {
    CGFloat radius = MIN(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) * 0.5;
    CGPoint center = CGPointMake(radius, radius);
    UIBezierPath* bezierPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    
    self.circle.bounds = self.bounds;
    self.circle.path = bezierPath.CGPath;
    self.circle.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    
    CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    CABasicAnimation* opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    CAAnimationGroup* animationGroup = [CAAnimationGroup animation];
    
    scaleAnimation.fromValue = @0.0;
    scaleAnimation.toValue = @1.0;
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    opacityAnimation.fromValue = @1.0;
    opacityAnimation.toValue = @0.0;
    opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:1 :0 :0.65 :1];
    
    animationGroup.duration = 1.0;
    animationGroup.repeatCount = INFINITY;
    animationGroup.animations = @[ scaleAnimation, opacityAnimation ];
    animationGroup.removedOnCompletion = NO;
    animationGroup.fillMode = kCAFillModeForwards;
    
    [self.circle addAnimation:animationGroup forKey:INSInfiniteIndicatorSpinAnimationKey];
    
    self.circle.transform = CATransform3DMakeScale(0, 0, 0);
    self.circle.opacity = 1.0;
    
    self.animating = YES;
}

- (void)stopAnimating {
    [self.circle removeAnimationForKey:INSInfiniteIndicatorSpinAnimationKey];
    self.circle.transform = CATransform3DMakeScale(0, 0, 0);
    self.circle.opacity = 1.0;
    
    self.animating = NO;
}

@end

